/*
Navicat SQL Server Data Transfer

Source Server         : LOCALHOST
Source Server Version : 110000
Source Host           : .:1433
Source Database       : AAACHAM2
Source Schema         : dbo

Target Server Type    : SQL Server
Target Server Version : 110000
File Encoding         : 65001

Date: 2014-06-09 17:56:11
*/


-- ----------------------------
-- Table structure for tb0113
-- ----------------------------
DROP TABLE [dbo].[tb0113]
GO
CREATE TABLE [dbo].[tb0113] (
[VoucherID] varchar(20) NOT NULL ,
[TableID] varchar(20) NOT NULL ,
[TranMonth] int NULL ,
[TranYear] int NULL ,
[DivisionID] varchar(20) NULL ,
[VoucherTypeID] varchar(20) NULL ,
[VoucherDate] datetime NULL ,
[VoucherNo] varchar(20) NULL ,
[ObjectID] varchar(20) NULL ,
[ProjectID] varchar(20) NULL ,
[OrderID] varchar(20) NULL ,
[BatchID] varchar(20) NULL ,
[WareHouseID] varchar(20) NULL ,
[ReDeTypeID] varchar(20) NULL ,
[Status] tinyint NOT NULL ,
[EmployeeID] varchar(20) NULL ,
[Description] varchar(250) NULL ,
[CreateDate] datetime NULL ,
[CreateUserID] varchar(50) NULL ,
[LastModifyUserID] varchar(50) NULL ,
[LastModifyDate] datetime NULL ,
[RefNo01] varchar(50) NULL ,
[RefNo02] varchar(50) NULL ,
[KindVoucherID] int NULL 
)


GO

-- ----------------------------
-- Records of tb0113
-- ----------------------------

-- ----------------------------
-- Indexes structure for table tb0113
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table tb0113
-- ----------------------------
ALTER TABLE [dbo].[tb0113] ADD PRIMARY KEY NONCLUSTERED ([VoucherID])
GO

-- ----------------------------
-- Triggers structure for table tb0113
-- ----------------------------
--DROP TRIGGER [dbo].[AX2026]
GO
CREATE TRIGGER [dbo].[AX2026]
ON [dbo].[tb0113]
AFTER DELETE
AS
Declare		@D26_Cursor		Cursor

Declare  	
	@DivisionID		Varchar(20),	
	@RDVoucherID		Varchar(20), 				
	@TranYear		Int,		
	@TranMonth		int


Set @D26_Cursor = Cursor Scroll KeySet For
	Select 	DivisionID, VoucherID, TranYear, TranMonth
	From 	Deleted


Open @D26_Cursor
Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth
                                         

While @@FETCH_STATUS = 0
Begin	
	
	DELETE FROM tb0104 WHERE DivisionID = @DivisionID and
				VoucherID = @RDVoucherID and
				TranYear = @TranYear and
				TranMonth = @TranMonth


	Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth                                       

End
Close @D26_Cursor
DeAllocate @D26_Cursor


GO
--DROP TRIGGER [dbo].[AY2026]
GO
CREATE TRIGGER [dbo].[AY2026]
ON [dbo].[tb0113]
AFTER INSERT
AS
Declare		@D26_Cursor		Cursor,
		@BaseCurrencyID as varchar(20)

Declare  	
	@DivisionID		Varchar(20),	
	@VoucherTypeID	Varchar(20),			
	@RDVoucherID		Varchar(20), 	@RDVoucherNo	Varchar(20),	
	@RDVoucherDate	Datetime,			
	@TranYear		Int,		@TranMonth		int,
	@WareHouseID		Varchar(20),	
	@Description		Varchar(255),	
	@CreateUserID		Varchar(20),	@CreateDate		Datetime,
	@LastModifyUserID	Varchar(20),	@LastModifyDate	Datetime,

	@EmployeeID		Varchar(20),	
	@ObjectID		Varchar(20),
	@Status		tinyint,
	@BatchID		varchar(20),
	@TableID 		Varchar(20),
	@OrderID		varchar(20),
	@RefNo01		varchar(50),
	@RefNo02		varchar(50)


Set @D26_Cursor = Cursor Scroll KeySet For
Select 		ins.DivisionID, ins.VoucherID, ins.TranYear, ins.TranMonth,   ---- Thong tin chung
		Ins.VoucherDate,  Ins.VoucherNo, 
		EmployeeID,  Ins.ObjectID,   Ins.CreateUserID, Ins.CreateDate,  
		Ins.LastModifyUserID, Ins.LastModifyDate, Description,		
		Ins.WareHouseID, 0, Ins.BatchID,
		Ins.TableID,
		Ins.VoucherTypeID,
		Ins.OrderID, RefNo01, RefNo02				

From 	inserted ins 


Open @D26_Cursor
Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth,   
		@RDVoucherDate,  @RDVoucherNo, 
		@EmployeeID,  @ObjectID,   @CreateUserID, @CreateDate,  
		@LastModifyUserID, @LastModifyDate, @Description,
		@WareHouseID, @Status, @BatchID, @TableID,
		@VoucherTypeID, @OrderID, @RefNo01, @RefNo02				
                                         

While @@FETCH_STATUS = 0
Begin	
	
	INSERT INTO tb0104 	(DivisionID, VoucherID, TranYear, TranMonth,
				VoucherDate, VoucherNo, KindVoucherID,
				EmployeeID, ObjectID, CreateUserID, CreateDate, 
				LastModifyUserID, LastModifyDate, Description,
				WareHouseID, Status, BatchID, TableID,
				VoucherTypeID, OrderID, RefNo01, RefNo02)
		VALUES	(@DivisionID, @RDVoucherID, @TranYear, @TranMonth,  
				@RDVoucherDate,  @RDVoucherNo, 2,
				@EmployeeID,  @ObjectID,   @CreateUserID, @CreateDate,  
				@LastModifyUserID, @LastModifyDate, @Description,
				@WareHouseID, @Status, @BatchID, 'tb0104',
				@VoucherTypeID, @OrderID, @RefNo01, @RefNo02)


	Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth,  
			@RDVoucherDate,  @RDVoucherNo, 
			@EmployeeID,  @ObjectID,   @CreateUserID, @CreateDate,  
			@LastModifyUserID, @LastModifyDate, @Description,
			@WareHouseID, @Status, @BatchID, @TableID,
			@VoucherTypeID, @OrderID, @RefNo01, @RefNo02				
                                       

End
Close @D26_Cursor
DeAllocate @D26_Cursor



GO
--DROP TRIGGER [dbo].[AZ2026]
GO
CREATE TRIGGER [dbo].[AZ2026]
ON [dbo].[tb0113]
AFTER UPDATE
AS
Declare		@D26_Cursor		Cursor,
		@BaseCurrencyID as varchar(20)

Declare  	
	@DivisionID		Varchar(20),	
	@VoucherTypeID	Varchar(20),			
	@RDVoucherID		Varchar(20), 	@RDVoucherNo	Varchar(20),	
	@RDVoucherDate	Datetime,			
	@TranYear		Int,		@TranMonth		int,
	@WareHouseID		Varchar(20),	
	@Description		Varchar(255),	
	@CreateUserID		Varchar(20),	@CreateDate		Datetime,
	@LastModifyUserID	Varchar(20),	@LastModifyDate	Datetime,
	@EmployeeID		Varchar(20),	
	@ObjectID		Varchar(20),
	@Status		tinyint,
	@BatchID		varchar(20),
	@TableID 		Varchar(20),
	@OrderID		varchar(20),
	@RefNo01		varchar(50),
	@RefNo02		varchar(50)


Set @D26_Cursor = Cursor Scroll KeySet For
Select 		ins.DivisionID, ins.VoucherID, ins.TranYear, ins.TranMonth,   ---- Thong tin chung
		Ins.VoucherDate,  Ins.VoucherNo, 
		EmployeeID,  Ins.ObjectID,   Ins.CreateUserID, Ins.CreateDate,  
		LastModifyUserID, LastModifyDate, Description,		
		Ins.WareHouseID, 0, Ins.BatchID,
		Ins.TableID,
		Ins.VoucherTypeID,
		Ins.OrderID, RefNo01, RefNo02				
From 	inserted ins 



Open @D26_Cursor
Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth,   
		@RDVoucherDate,  @RDVoucherNo, 
		@EmployeeID,  @ObjectID,   @CreateUserID, @CreateDate,  
		@LastModifyUserID, @LastModifyDate,@Description,
		@WareHouseID, @Status, @BatchID, @TableID,
		@VoucherTypeID, @OrderID, @RefNo01, @RefNo02				
                                         

While @@FETCH_STATUS = 0
Begin	
	
	Update tb0104 		Set 	VoucherDate = @RDVoucherDate,
					VoucherNo = @RDVoucherNo,
					EmployeeID = @EmployeeID,
					ObjectID = @ObjectID,
					LastModifyUserID = @LastModifyUserID, 
					LastModifyDate = @LastModifyDate,
					Description = @Description,
					WareHouseID = @WareHouseID,
					VoucherTypeID = @VoucherTypeID ,
					OrderID = @OrderID,
					RefNo01 = @RefNo01, 
					RefNo02 = @RefNo02 
	Where VoucherID = @RDVoucherID and TranMonth = @TranMonth and TranYear = @TranYear and DivisionID =@DivisionID and TableID = 'tb0104'


							

	Fetch Next From @D26_Cursor Into @DivisionID, @RDVoucherID, @TranYear, @TranMonth,  
			@RDVoucherDate,  @RDVoucherNo, 
			@EmployeeID,  @ObjectID,   @CreateUserID, @CreateDate,  
			@LastModifyUserID, @LastModifyDate,@Description,
			@WareHouseID, @Status, @BatchID, @TableID,
			@VoucherTypeID, @OrderID, @RefNo01, @RefNo02				
                                       

End
Close @D26_Cursor
DeAllocate @D26_Cursor





GO
